<?php
namespace app\common\service;

use app\common\model\Product as ProductModel;
use think\Loader;

class Product{
    public function paginate($pageSize = 20){
        $productModel = new ProductModel();

        $products = $productModel -> order('id desc')->paginate($pageSize);

        return $products;
    }

    public function find($id){
        $productModel = new ProductModel();
        return $productModel -> where('id',$id)->find();
    }


    public function insert($data){

        $productModel = new ProductModel();

        $validate = Loader::validate("ProductValidate");
        $checkResult = $validate ->scene("add") -> check($data);
        if(!$checkResult){
            return ['state'=>0,'message'=>$validate->getError()];
        }

        $result = $productModel->allowField(['name','sys_cat','fun_cat','cover'])->save($data);


        if(false === $result){

            return ['state'=>0,'message'=>$productModel->getError()];
        }

        return ['state'=>1,'message'=>'','insertId'=>$productModel->id];

    }

}